package xyz.zq.sf.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.support.ServerWebExchangeUtils;
import org.springframework.core.Ordered;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
import xyz.zq.sf.common.core.constant.SecurityConstants;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 全局拦截器，作用所有的微服务
 * 对请求头中参数进行处理 from 参数进行清洗 2. 重写StripPrefix = 1,支持全局
 * 支持swagger添加X-Forwarded-Prefix header （F SR2 已经支持，不需要自己维护）
 *
 * @author hzq
 * @date 2021-02-05 15:18
 */
@Component
public class RequestGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        /*1. 清洗请求头中from 参数*/
        ServerHttpRequest request = exchange
                .getRequest()
                .mutate()
                .headers(httpHeaders -> httpHeaders.remove(SecurityConstants.FROM))
                .build();

        /*2. 重写StripPrefix*/
        ServerWebExchangeUtils.addOriginalRequestUrl(exchange, request.getURI());

        String rawPath = request.getURI().getRawPath();

        String newPath = "/" + Arrays.stream(StringUtils.tokenizeToStringArray(rawPath, "/"))
                .skip(1L).collect(Collectors.joining("/"));

        ServerHttpRequest newRequest = request.mutate().path(newPath).build();

        exchange.getAttributes().put(ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR, newRequest.getURI());

        return chain.filter(exchange.mutate().request(newRequest.mutate().build()).build());
    }

    @Override
    public int getOrder() {
        return -1000;
    }
}
